//#include <iostream>
#include <cassert>
#include <cmath>
#include <algorithm>
#include "array.h"

const float array::COEF = 2.0f;

array::array( size_t size )
:   size_       (size),
    capacity_   (size)
{
    assert (size>0);
    ptr_ = new data_type[capacity_];
    std::fill (ptr_, ptr_ + size_, 0);
}

array::array( const array& arr )
:   size_       (arr.size_),
    capacity_   (arr.capacity_)
{
    ptr_ = new data_type[capacity_];
    std::copy (arr.ptr_, arr.ptr_ + size_, ptr_);
}

array& array::operator=( const array& arr )
{
    if (this != &arr)
    {   
        array tmp (arr);
        swap (tmp);
    }
    return *this;
}

array::~array()
{
    delete [] ptr_;

}


void array::swap( array& arr )
{
    std::swap (ptr_,        arr.ptr_);
    std::swap (size_,       arr.size_);
    std::swap (capacity_,   arr.capacity_);
}


array::data_type array::get( size_t index ) const
{
    assert (index < size_);
    return ptr_[index];
}

void array::set( size_t index, data_type value )
{
    assert (index < size_);
    ptr_[index] = value;
}

void array::push_back( data_type value )
{
    if (size_==capacity_)
        resize ();

    assert (capacity_ > size_);
    
    ++size_;
    set (size_-1, value);
}

void array::resize()
{
    size_t new_cap = static_cast<size_t>(ceil(capacity_*COEF));
    
//    std::cout << "Resize: " << capacity_ << " -> " << new_cap << "\n";
    
    capacity_ = new_cap;
    
    data_type *tmp_ptr = new data_type[capacity_];
    std::copy (ptr_, ptr_ + size_, tmp_ptr);
    
    delete [] ptr_;
    ptr_ = tmp_ptr;
}

size_t array::size() const
{
    return size_;
}

